package com.wkbb.basic.controller.manage;

import com.github.pagehelper.PageInfo;
import com.wkbb.basic.model.VersionControl;
import com.wkbb.basic.service.DeviceVersionControlService;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.DeviceDetailDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.dto.basic.VersionControlDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhichao
 * @date 2019/8/22
 * @description
 */
@RestController
@Api(description = "设备版本管理(后台管理)")
@RequestMapping("/basic/{apiVersion}/device/version/manage")
@Slf4j
public class DeviceVersionManageController extends BaseController {

    @Autowired
    DeviceVersionControlService deviceVersionControlService;

    @ApiOperation(value = "查询版本升级信息分页列表")
    @RequestMapping(value = "/getDeviceVersionPageInfoList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<VersionControl>> getDeviceVersionPageInfoList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "目标类型", required = false) @RequestParam(name = "type", required = false) Integer type,
            @ApiParam(value = "页码",required = false,defaultValue = "1") @RequestParam(name = "pageNo",required = false,defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数",required = false,defaultValue = "10") @RequestParam(name = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        log.info("***********DeviceVersionManageController.getDeviceVersionPageInfoList******apiVersion:{},type:{}", apiVersion, type);
        try {
            UserDto userDto = getUserByLogin();
            if (userDto == null) {
                return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
            }

            Map<String,Object> parms = new HashMap<>();
            parms.put("type", type);
            return new ResultDTO<>(deviceVersionControlService.getDeviceVersionPageInfoList(parms ,pageNo ,pageSize));
        } catch (Exception e) {
            log.error("***********DeviceVersionManageController.getDeviceVersionPageInfoList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "查询设备详情分页列表")
    @RequestMapping(value = "/getDeviceListByMapParms", method = RequestMethod.GET)
    public ResultDTO<PageInfo<DeviceDetailDto>> getDeviceListByMapParms(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "版本控制id", required = true) @RequestParam(name = "id", required = true) Long id,
            @ApiParam(value = "所在学校", required = false) @RequestParam(name = "orgName", required = false) String orgName,
            @ApiParam(value = "设备类型", required = false) @RequestParam(name = "deviceType", required = false) String deviceType,
            @ApiParam(value = "设备名称", required = false) @RequestParam(name = "deviceName", required = false) String deviceName,
            @ApiParam(value = "设备编号", required = false) @RequestParam(name = "deviceCode", required = false) String deviceCode,
            @ApiParam(value = "页码",required = false,defaultValue = "1") @RequestParam(name = "pageNo",required = false,defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数",required = false,defaultValue = "10") @RequestParam(name = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        log.info("***********DeviceVersionManageController.getDeviceListByMapParms******apiVersion:{},id:{}", apiVersion, id);
        try {
            UserDto userDto = getUserByLogin();
            if (userDto == null) {
                return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
            }
            Map<String,Object> parms = new HashMap<>();
            parms.put("id", id);
            if(orgName!=null){
                parms.put("orgName", orgName);
            }
            if(deviceType!=null){
                parms.put("deviceType", deviceType);
            }
            if(deviceName!=null){
                parms.put("deviceName", deviceName);
            }
            if(deviceCode!=null){
                parms.put("deviceCode", deviceCode);
            }
            return new ResultDTO<>(deviceVersionControlService.getDeviceListByMapParms(pageNo ,pageSize,parms ));
        } catch (Exception e) {
            log.error("***********DeviceVersionManageController.getDeviceListByMapParms******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "新增版本升级信息")
    @RequestMapping(value = "/addDeviceVersion", method = RequestMethod.POST)
    public ResultDTO<VersionControlDto> addDeviceVersion(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "版本控制信息", required = true) @RequestBody VersionControlDto versionControlDto) {
        log.info("***********DeviceVersionManageController.addDeviceVersion******apiVersion:{},versionControlDto:{}", apiVersion, versionControlDto);
        try {
            UserDto userDto = getUserByLogin();
            if (userDto == null) {
                return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
            }
            if(versionControlDto.getTargetIdList()==null &&versionControlDto.getTargetIdList().isEmpty() || versionControlDto.getType() == null
              || versionControlDto.getFileUrl() == null || versionControlDto.getFileName() == null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            deviceVersionControlService.addDeviceVersion(versionControlDto);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********DeviceVersionManageController.addDeviceVersion******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "更新版本升级信息")
    @RequestMapping(value = "/updateDeviceVersion", method = RequestMethod.PUT)
    public ResultDTO<VersionControlDto> updateDeviceVersion(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备信息", required = true) @RequestBody VersionControlDto versionControlDto) {
        log.info("***********DeviceVersionManageController.updateDeviceVersion******apiVersion:{},versionControlDto:{}", apiVersion, versionControlDto);
        try {
            UserDto userDto = getUserByLogin();
            if (userDto == null) {
                return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
            }
            deviceVersionControlService.updateDeviceVersion(versionControlDto);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********DeviceVersionManageController.updateDeviceVersion******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据id修改版本状态（启用，禁用）")
    @RequestMapping(value = "/updateStatusById", method = RequestMethod.PUT)
    public ResultDTO<Object> updateStatusById(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "状态", required = true) @RequestParam(name = "status", required = true) Boolean status,
            @ApiParam(value = "目标id", required = true) @RequestParam(name = "id", required = true) Long id) {
        log.info("***********DeviceVersionManageController.updateStatusById******apiVersion:{},id:{}", apiVersion, id);
        try {
            UserDto userDto = getUserByLogin();
            if (userDto == null) {
                return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
            }
            Map<String,Object> parms = new HashMap<>();
            parms.put("id", id);
            parms.put("status", status?1:0);
            deviceVersionControlService.updateStatusById(parms);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********DeviceVersionManageController.updateStatusById******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "删除版本升级信息")
    @RequestMapping(value = "/deleteDeviceVersion", method = RequestMethod.DELETE)
    public ResultDTO<Object> deleteDeviceVersion(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "设备信息id", required = true) @RequestParam(name = "ids", required = true) List<Long> ids) {
        log.info("***********DeviceVersionManageController.deleteDeviceVersion******apiVersion:{},ids:{}", apiVersion, ids);
        try {
            UserDto userDto = getUserByLogin();
            if (userDto == null) {
                return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
            }
            deviceVersionControlService.deleteDeviceVersionByIdBatch(ids);
            return new ResultDTO<>();
        } catch (Exception e) {
            log.error("***********DeviceVersionManageController.deleteDeviceVersion******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

}


